package com.android.sreader;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.android.sreader.LibraryGridView.MyOnItemClickListener;
import com.android.sreader.utility.Book;
import com.android.sreader.utility.Library;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SReaderLibraryActivity extends Activity{
	
	//private static List<String> list = null;
	private final static String TAG = SReaderApplication.LogLibrary;
	private final static SReaderApplication mApp = SReaderApplication.Instance();
    
    public static class DragGridAdapter extends ArrayAdapter<Book>{

    	public DragGridAdapter(Context context, List<Book> objects) {
            super(context, 0, objects);
        }
    	
		public List<Book> getList(){
            return library.getBooks();
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if(view==null){
                view = LayoutInflater.from(getContext()).inflate(R.layout.drag_grid_item, null);
            }
            TextView tv = (TextView) view.findViewById(R.id.drag_grid_item_text);
            tv.setText(library.getBook(position).getBookName());
            ImageView iv = (ImageView) view.findViewById(R.id.drag_grid_item_drag);
            iv.setImageDrawable(library.getBook(position).getBookCover());
            return view;
        }
    }
    
    LibraryGridView dragGridView = null;
    private DragGridAdapter adapter = null;
    private static Library library  = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.library);
        
        if(library == null) library = new Library();
        initData();        
        
        dragGridView = (LibraryGridView)findViewById(R.id.drag_grid);
        adapter = new DragGridAdapter(this,library.getBooks());
        dragGridView.setAdapter(adapter);
        dragGridView.setOnItemClickListener(new MyOnItemClickListener(){
			public void onItemClick(int pos) {
				Intent intent = new Intent();
				intent.setClass(SReaderLibraryActivity.this, SReaderBookViewActivity.class);
				intent.putExtra("BookPosition",pos);
				startActivity(intent);	
			}

        });
        
        Button refreshButton = (Button) this.findViewById(R.id.refresh_button);
        refreshButton.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				refreshData();
			}
        	
        });
        
        try {
        	mApp.light = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
			//Log.v(TAG, Integer.toString(light));
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mApp.light = 102; //default what ever
		} 
        
        Button lightButton = (Button) this.findViewById(R.id.light_button);
        lightButton.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				showDialog(DIALOG_LIGHT_SETTING);
			}
        	
        });
       
        
        /*dragGridView.setOnScrollListener(new OnScrollListener(){

			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}

			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				// TODO Auto-generated method stub
				
			}
        	
        });*/
    }
    
    protected void refreshData() {
		this.dragGridView.setAdapter(null);
		this.library.clear();
		this.showDialog(DIALOG_LOADDING);
		initData();
		adapter = new DragGridAdapter(this,library.getBooks());
        dragGridView.setAdapter(adapter);
        this.dismissDialog(DIALOG_LOADDING);
	}

	public void initData(){
        //数据结果
    	List<String> files = new ArrayList<String>();
		File file = new File(SReaderApplication.bookPath);
		if(!file.isDirectory()) return;
		
		File[] allFile = file.listFiles();
		for(int i = 0 ; i < allFile.length; i++){
			file = allFile[i];
			if(file.isFile() && file.getName().indexOf(".epub") > -1){
				//files.add(file.getPath());
				Book book = new Book();
				book.initWithPath(file.getPath());
				Library.books.add(book);
			}
		}
    }
    
    private final int MenuBase = Menu.FIRST;
	private final int Setting  = Menu.FIRST;
	private final int Search   = Menu.FIRST+1;
	private static final int DIALOG_PROGRESS = 1;
	private static final int DIALOG_LOADDING = 2;
	private static final int DIALOG_LIGHT_SETTING = 3;
	
	private ProgressDialog mProgressDialog;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	menu.add(MenuBase,Setting, Setting, R.string.setting);
    	menu.add(MenuBase,Search,Search,R.string.search);
    	return true;
    }
    
    OnSeekBarChangeListener lightSeekBarListener = new OnSeekBarChangeListener() {

		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			if(fromUser){
				
			}
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

     };
    
    @Override
    protected Dialog onCreateDialog(int id) {
    	switch (id) {
    		case DIALOG_PROGRESS:
    			mProgressDialog = new ProgressDialog(this);
    			mProgressDialog.setMessage("请稍候");
    			mProgressDialog.setIndeterminate(true);
    			mProgressDialog.setCancelable(true);
    			return mProgressDialog;
    		case DIALOG_LOADDING:
    			mProgressDialog = new ProgressDialog(this);
    			mProgressDialog.setMessage("请稍候");
    			mProgressDialog.setIndeterminate(true);
    			mProgressDialog.setCancelable(true);
    			return mProgressDialog;
    		case DIALOG_LIGHT_SETTING:
    			Dialog lightDialog = new Dialog(this);
    			LayoutInflater inflater =  (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    			View layout = inflater.inflate(R.layout.light_setting, (ViewGroup)findViewById(R.id.light_setting));
    			lightDialog.setContentView(layout);
    			lightDialog.setTitle("拖动以改变屏幕亮度");
    			SeekBar lightDialogSeekBar = (SeekBar) lightDialog.findViewById(R.id.light_seekbar);
    			lightDialogSeekBar.setMax(253);
    			lightDialogSeekBar.setOnSeekBarChangeListener(lightSeekBarListener);
    			return lightDialog;
    		default:
    			return null;
    	}
    }
}
